//-----------------------------------------------------------------------
// <copyright file="DanhMucDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using APPro.Entities;

using Pyco;
using Pyco.Diagnostics;
using Pyco.Data;

namespace APPro.Data
{    
    public abstract class DanhMucDAOBase
    {
        #region Common methods
        public virtual DanhMuc CreateDanhMucFromReader(IDataReader reader)
        {
            DanhMuc item = new DanhMuc();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("MaDanhMuc"))) item.MaDanhMuc = (int)reader["MaDanhMuc"];
                if (!reader.IsDBNull(reader.GetOrdinal("TenDanhMuc"))) item.TenDanhMuc = (string)reader["TenDanhMuc"];
                if (!reader.IsDBNull(reader.GetOrdinal("ThuocDanhMuc"))) item.ThuocDanhMuc = (int)reader["ThuocDanhMuc"];
                if (!reader.IsDBNull(reader.GetOrdinal("LoaiDanhMuc"))) item.LoaiDanhMuc = (int)reader["LoaiDanhMuc"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateDanhMucFromReaderException, ex);
            }
            return item;
        }
        #endregion
		
        #region CreateDanhMuc methods
            
        public virtual void CreateDanhMuc(DanhMuc danhMuc)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spDanhMucInsert");
                
				database.AddInParameter(dbCommand, "@MaDanhMuc", DbType.Int32, danhMuc.MaDanhMuc);
				database.AddInParameter(dbCommand, "@TenDanhMuc", DbType.String, danhMuc.TenDanhMuc);
				database.AddInParameter(dbCommand, "@ThuocDanhMuc", DbType.Int32, danhMuc.ThuocDanhMuc);
				database.AddInParameter(dbCommand, "@LoaiDanhMuc", DbType.Int32, danhMuc.LoaiDanhMuc);
				
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateDanhMucException, ex);
            }
        }

        #endregion
		
        #region UpdateDanhMuc methods
        
        public virtual void UpdateDanhMuc(DanhMuc danhMuc)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spDanhMucUpdate");            
                
                database.AddInParameter(dbCommand, "@MaDanhMuc", DbType.Int32, danhMuc.MaDanhMuc);
                database.AddInParameter(dbCommand, "@TenDanhMuc", DbType.String, danhMuc.TenDanhMuc);
                database.AddInParameter(dbCommand, "@ThuocDanhMuc", DbType.Int32, danhMuc.ThuocDanhMuc);
                database.AddInParameter(dbCommand, "@LoaiDanhMuc", DbType.Int32, danhMuc.LoaiDanhMuc);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessUpdateDanhMucException, ex);
            }
        }
        
        #endregion
		
        #region DeleteDanhMuc methods
        public virtual void DeleteDanhMuc(int maDanhMuc)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spDanhMucDelete");
                
                database.AddInParameter(dbCommand, "@MaDanhMuc", DbType.Int32, maDanhMuc);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessDeleteDanhMucException, ex);
            }
        }

        #endregion
		
        #region GetDanhMuc methods
        
        public virtual DanhMuc GetDanhMuc(int maDanhMuc)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spDanhMucGet");
                
                database.AddInParameter(dbCommand, "@MaDanhMuc", DbType.Int32, maDanhMuc);
                
                DanhMuc danhMuc =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        danhMuc = CreateDanhMucFromReader(reader);
                        reader.Close();
                    }
                }
                return danhMuc;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetDanhMucException, ex);
            }
        }
        
        #endregion
		
        #region GetDanhMucList methods
        public virtual DanhMucCollection GetDanhMucList(DanhMucColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spDanhMucGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                DanhMucCollection danhMucCollection = new DanhMucCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        DanhMuc danhMuc = CreateDanhMucFromReader(reader);
                        danhMucCollection.Add(danhMuc);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return danhMucCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetDanhMucListException, ex);
            }
        }
        
        public virtual DanhMucCollection GetDanhMucList(DanhMucColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetDanhMucList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
    }
}